﻿@model PagedList.IPagedList<CP_SchoolForChildren_v0._1.Models.RelatedModel.SeachSchoolModel>
@using CP_SchoolForChildren_v0._1.Models.DataModel;

@{
    SFCEntities db = new SFCEntities();
    int count = ViewBag.Count;
}
    <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCeFkS9kOQDDQHkqnokJTvQKUwJo14huzE&sensor=false" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/FE/style/SchoolStyle2.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/FE/Addition.css")" rel="stylesheet" type="text/css" />
<style type="text/css">
    #sort-div {
        padding: 10px;
        line-height: 20px;
        border-bottom: 1px dotted rgba(119, 79, 56, 0.4);
    }
    #sort-div h2 {
        margin-bottom: 10px;
    }
    #sort-div span {
        padding-left: 5px;
    }
    #sort-div span a {
        font-weight: normal;
    }
    #sort-div span a:hover {
        font-weight: bold;
    }
    .sperator {
        border-right: 1px solid rgba(119, 79, 56, 0.4);
        padding-left: 3px;
        padding-right: 3px;
        width: 10px;
    }
    #sort-div span.sort-active a {
        font-weight: bold;
    }
    #CityID, #DistrictID, #SchoolTypeID, #Fee {
        background: none repeat scroll 0 0 padding-box rgba(255, 255, 255, 0.9);
        border-color: rgba(23, 51, 78, 0.15) rgba(23, 51, 78, 0.17) rgba(23, 51, 78, 0.2);
        border-image: none;
        border-radius: 2.5px 0 0 2.5px;
        border-style: solid;
        border-width: 1px;
        box-shadow: 0 1px 0 rgba(8, 22, 37, 0.02) inset, 0 0 2px rgba(8, 22, 37, 0.1) inset, 0 1px 0 rgba(255, 255, 255, 0.2);
        padding: 5px 8px;
        width: 260px;
        margin: 15px;
        margin-bottom: 0px;
    }
    #ListService {
        line-height: 28px;
        padding: 5px 8px;
        width: 260px;
        height: 150px;
        overflow: auto;
        margin: 15px;
        border: 1px solid;
        border-color: rgba(23, 51, 78, 0.15) rgba(23, 51, 78, 0.17) rgba(23, 51, 78, 0.2);
    }
</style>

<img id="spinner" src="@Url.Content("~/Content/images/loading.gif")" style="display: none;">
<div id="search-result" >
    <div class="content-main" style="width: 650px;">
        <div class="content-header">
            <div style="padding: 10px;">
                <input type="text" value="" id="txtAddress" data-url="@Url.Action("SearchByMap", "School")" placeholder="Nhập địa điểm tại đây" style="width: 250px;"/>
                <input type="submit" id="btnSearch" class="btn" value="Tìm kiếm" style="height: 32px"/>
            </div>
            <div id="map_canvas">
            </div>
        </div>
    </div>   
    <div class="content-main" style="width: 650px;">
        <div class="content-header">
            <div id="sort-div">
                <div>
                    <span>Sắp xếp theo: </span>
                    <span id="name-sort">
                        @Ajax.ActionLink("Tên trường ", "SearchByMap","School", new {ViewBag.lat, ViewBag.lng,ViewBag.radius, sortOrder = ViewBag.NameSortParm}, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                    </span>
                    <span class="sperator"></span>
                    <span id="rate-sort">
                        @Ajax.ActionLink("Chất lượng", "SearchByMap", "school", new { ViewBag.lat, ViewBag.lng, ViewBag.radius, sortOrder = ViewBag.RateSortParm}, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                    </span>
                    <span class="sperator"></span>
                    <span id="fee-sort">@Ajax.ActionLink("Học phí", "SearchByMap", "School", new { ViewBag.lat, ViewBag.lng, ViewBag.radius, sortOrder = ViewBag.FeeSortParm}, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })</span>
                </div>
                <div>
            @if (Model.PageCount > 1)
            {
                <div style="padding: 10px;">
                    <div class="paging" style="float: right;">
                        Trang <span style="font-weight: bold">@(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)</span>
                        / <span style="font-weight: bold">@Model.PageCount</span>
     
                        @if (Model.HasPreviousPage)
                        {
                            @Ajax.ActionLink("Trang đầu", "SearchByMap", "school", new { page = 1, ViewBag.lat, ViewBag.lng, ViewBag.radius, sortOrder = ViewBag.CurrentSort }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                            @Html.Raw(" ")
                            ;
                            @Ajax.ActionLink("<", "SearchByMap", "School", new { page = Model.PageNumber - 1, ViewBag.lat, ViewBag.lng, ViewBag.radius, sortOrder = ViewBag.CurrentSort }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                        }
     
                        @if (Model.HasNextPage)
                        {
                            @Ajax.ActionLink(">", "SearchByMap","School", new { page = Model.PageNumber + 1,ViewBag.lat, ViewBag.lng,ViewBag.radius, sortOrder = ViewBag.CurrentSort }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                            @Html.Raw(" ")
                            ;
                            @Ajax.ActionLink("Trang cuối", "SearchByMap", "School", new { page = Model.PageCount, ViewBag.lat, ViewBag.lng, ViewBag.radius, sortOrder = ViewBag.CurrentSort }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                        }
                    </div>
                    <br class="spacer"/>
                </div>
            }

            <br class="spacer">
        </div>
            </div>
        </div>
 
        <div class="boxMod">
            <div id="sort-div">
                <h2>
                    Tìm thấy @count kết quả
                    <!--span style="float: right; font-size: 14px;">
                        Hiển thị 
                    </span -->
                    <br class="spacer" />
                </h2>                
            </div>               
                    
            @foreach (var item in Model)
            {
                string host = Dns.GetHostName();
                IPHostEntry ip = Dns.GetHostEntry(host);
                string strClientIP = ip.AddressList[0].ToString();
                var m = (from rs in db.ReportSchools
                         where rs.SchoolID == item.SchoolID && rs.AuthorIP == strClientIP
                         select rs.ReportSchoolID).ToArray();
                <div id="result">
                    <div class="title">
                        @Html.ActionLink(item.SchoolName, "Detail", "School", new {schoolID = @item.SchoolID}, new {@class = "itemname"})
                    </div>
                    <div class="info">
                        @item.TypeName
                        <br />
                        @item.Address - Quận @item.DistrictName - TP. @item.CityName
                        <br />
                        <span style="color: #774F38; font-weight: bold; font-size: 11px">Học phí: </span>@item.TuitionFee 000 vnđ
                    </div>
                    <div class="rank">
                        Đánh giá của hệ thống:
                        <br/>
                        @if (@item.SystemRate < 1.5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@item.SystemRate == 1.5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Half" />
                            <input type="button" class="Empty"/>
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@item.SystemRate > 1.5 && @item.SystemRate < 2.5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Empty"/>
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@item.SystemRate == 2.5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Half"/>
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@item.SystemRate > 2.5 && @item.SystemRate < 3.5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled"/>
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@item.SystemRate == 3.5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled"/>
                            <input type="button" class="Half" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@item.SystemRate > 3.5 && @item.SystemRate < 4.5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled"/>
                            <input type="button" class="Filled" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@item.SystemRate == 4.5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled"/>
                            <input type="button" class="Filled" />
                            <input type="button" class="Half"/>
                        }
                        else if (@item.SystemRate > 4.5 && @item.SystemRate <= 5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled"/>
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled"/>
                        }
                        <br/>
                        @{
                            var socirate = (from sv in db.SocialRatings
                                            where sv.SchoolID == item.SchoolID
                                            select sv.SocialRate).ToArray();
                            double socialrate = 0, totalStar = 0;
                            for (int f = 0; f < socirate.Count(); f++)
                            {
                                totalStar += socirate[f];
                            }
                            int totalSoci = socirate.Count();
                            socialrate = totalStar/totalSoci;
                        }
                        Đánh giá của cộng đồng:
                        <br />
                        @if (@totalSoci == 0)
                        {
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@socialrate < 1.25)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@socialrate >= 1.25 && @socialrate < 1.75)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Half"/>
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty"/>
                        }
                        else if (@socialrate >= 1.75 && @socialrate < 2.25)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty" />
                        }
                        else if (@socialrate >= 2.25 && @socialrate < 2.75)
                        {
                            <input type="button" class="Filled"/>
                            <input type="button" class="Filled" />
                            <input type="button" class="Half" />
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty" />
                        }
                        else if (@socialrate >= 2.75 && @socialrate < 3.25)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled"/>
                            <input type="button" class="Empty" />
                            <input type="button" class="Empty" />
                        }
                        else if (@socialrate >= 3.25 && @socialrate < 3.75)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Half" />
                            <input type="button" class="Empty" />
                        }
                        else if (@socialrate >= 3.75 && @socialrate < 4.25)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled"/>
                            <input type="button" class="Filled" />
                            <input type="button" class="Empty" />
                        }
                        else if (@socialrate >= 4.25 && @socialrate < 4.75)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Half" />
                        }
                        else if (@socialrate >= 4.75 && @socialrate <= 5)
                        {
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled" />
                            <input type="button" class="Filled"/>
                            <input type="button" class="Filled" />
                        }
                        <br />
                        (@totalSoci lượt đánh giá)
                    </div>
                    <br class="spacer" />
                    <div class="bottom-control">
                        <div class="compare-cp">
                            <label id="compare_text">
                                <input type="checkbox" class="compare_check" id="compare_check" name="compare_check" value="@item.SchoolID" style="vertical-align: middle"/>
                                <span id="letcompare" value="@item.SchoolID">So sánh</span>
                            </label>
                        </div>
                        <div id="add_to_favourite">
                            <span><input type="button" id="submit_btn2" name="@item.SchoolID" title="Thêm vào danh sách ưa thích" data-url="@Url.Action("AddToFavourite", "School")" lang="@User.Identity.Name" />Ưa thích</span>
                            <br class="spacer"/>
                        </div>
                        <div id="report_school">
                            <span><input type="submit" id="submit_btn1" name="@item.SchoolID" title="Báo cáo vi phạm trường này" accesskey="@m.Count()" value="" data-url="@Url.Action("Report", "School")" lang="@User.Identity.Name"/> Vi phạm</span>
                            <br class="spacer"/>
                        </div>
                        <br class="spacer" />
                                
                    </div>
                    <br class="spacer" />
                </div>
            }
            <br class="spacer"/>
               
            <p class="rightBottom"></p>
            <br class="spacer" />
        </div>
        <br class="spacer"/>
        <div>
            @if (Model.PageCount > 1)
            {
                <div style="padding: 10px;">
                    <div class="paging" style="float: right;">
                        Trang <span style="font-weight: bold">@(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)</span>
                        / <span style="font-weight: bold">@Model.PageCount</span>
     
                        @if (Model.HasPreviousPage)
                        {
                            @Ajax.ActionLink("Trang đầu", "SearchByMap", "school", new { page = 1, ViewBag.lat, ViewBag.lng, ViewBag.radius, sortOrder = ViewBag.CurrentSort }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                            @Html.Raw(" ")
                            ;
                            @Ajax.ActionLink("<", "SearchByMap", "School", new { page = Model.PageNumber - 1, ViewBag.lat, ViewBag.lng, ViewBag.radius, sortOrder = ViewBag.CurrentSort }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                        }
     
                        @if (Model.HasNextPage)
                        {
                            @Ajax.ActionLink(">", "SearchByMap","School", new { page = Model.PageNumber + 1,ViewBag.lat, ViewBag.lng,ViewBag.radius, sortOrder = ViewBag.CurrentSort }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                            @Html.Raw(" ")
                            ;
                            @Ajax.ActionLink("Trang cuối", "SearchByMap", "School", new { page = Model.PageCount, ViewBag.lat, ViewBag.lng, ViewBag.radius, sortOrder = ViewBag.CurrentSort }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "search-result", InsertionMode = InsertionMode.Replace })
                        }
                    </div>
                    <br class="spacer"/>
                </div>
            }

            <br class="spacer">
        </div>
    </div>
</div>
<!-- Search compare -->
<script type="text/javascript">
    $(document).ready(function () {
        $('#compare_text #letcompare #compare-now').click(function (event) {
            event.preventDefault();
        });


        var selected = new Array();
        $('input.compare_check').bind('change', function () {

            if ($(this).attr("checked")) {
                if (selected.length == 4) {
                    alert('Bạn không được chọn quá 4 trường');
                    $(this).attr('checked', false);
                    return false;
                }
                selected.push($(this).attr("value"));
            } else {
                $(this).innerHTML = "So sánh";
                Array.prototype.remove = function (v) { this.splice(this.indexOf(v) == -1 ? this.length : this.indexOf(v), 1); };
                selected.remove($(this).attr("value"));
                var temp = $('#compare_text #letcompare');
                if (selected.length == 0) {
                    for (var k = 0; k < temp.length; k++) {
                        temp[k].innerHTML = "So sánh";
                    }
                }

            }
            var url = "";
            if (selected.length > 1) {
                url = '<a href="' +
                    '@Url.Action("Compare","School")' + '?ListSchool=' + selected + '" id="compare-now">So sánh ngay</a>';
                var list = $('#compare_text #letcompare');
                for (var i = 0; i < list.length; i++) {
                    //console.log(list[i]);
                    for (var j = 0; j < selected.length; j++) {
                        if (list[i].attributes[0].value == selected[j]) {
                            list[i].innerHTML = url;
                        }
                    }
                }
            } else {
                url = "So sánh";
                var list = $('#compare_text #letcompare');
                for (var i = 0; i < list.length; i++) {
                    //console.log(list[i]);
                    for (var j = 0; j < selected.length; j++) {
                        if (list[i].attributes[0].value != selected[j]) {
                            list[i].innerHTML = url;
                        }
                    }
                }
            }


        });
        //  

    });
</script>

<script type="text/javascript">
    $(document).ready(function () {
        initialize();
    });
</script>
<script type="text/javascript">
    function initialize() {

        var map;
        var geocoder = new google.maps.Geocoder();
        var lat = get_param('lat');
        var lng = get_param('lng');
        var myOptions = {
            zoom: 13,
            center: new google.maps.LatLng(lat, lng),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng),
            icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png',
            draggable: true,
            map: map
        });

        GetReady(map);



        function placeMarker(location) {
            if (marker) {
                marker.setPosition(location);
            } else {
                marker = new google.maps.Marker({
                    position: location,
                    icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png',
                    draggable: true,
                    map: map
                });
            }

            var slocaton;
            geocoder.geocode({ 'latLng': location }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    slocaton = results[1].formatted_address;
                    $('#txtAddress').val(slocaton);
                } else {
                    alert("Geocoder failed due to: " + status);
                }
            });
        }
        google.maps.event.addListener(map, 'click', function (event) {
            placeMarker(event.latLng);
        });

        $("#btnSearch").click(function () {
            convertAddressToLatLng($("#txtAddress").val());
        });
    };

    function convertAddressToLatLng(address) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                //Empty div before re-populating
                //$("#divStores").html('');
                //searchStores(results[0].geometry.location);
                var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());

                //hard coded the radius to 2 km, if you get the value from a field if required
                var $parameters = 'lat=' + results[0].geometry.location.lat() + '&lng=' + results[0].geometry.location.lng() + '&radius=2';
                $.ajax({
                    url: $("#txtAddress").attr("data-url"),
                    type: 'GET',
                    data: $parameters,
                    success: function () {
                        location.href = ('@Url.Action("SearchByMap","School")' + '?lat=' + results[0].geometry.location.lat() + '&lng=' + results[0].geometry.location.lng() + '&radius=2');
                    }
                });
            } else {
                alert("Vui lòng nhập địa chỉ hoặc kiểm tra lại địa chỉ đã nhập");
            }
        });
    }

    function GetReady(map) {
        var lat = get_param('lat');
        var lng = get_param('lng');
        var radius = get_param('radius');

        var url2 = '@Url.Action("GetThePoint", "School")' + '?lat=' + lat + '&lng=' + lng + '&radius=' + radius;
        $.getJSON(url2, { ajax: 'true' }, function (response) {
            var result = response.Result;
            if (result == 'OK') {
                var rows = response.Record;
                for (var i = 0; i < rows.length; i++) {
                    var tmp = rows[i].API.split(',');
                    var myLatlng = new google.maps.LatLng(tmp[0], tmp[1]);
                    var marker = new google.maps.Marker({
                        position: myLatlng,
                        map: map
                    });
                }
            } else {
                alert(response.message);
            }
        });
    }
    function get_param(param) {
        var search = window.location.search.substring(1);
        var compareKeyValuePair = function (pair) {
            var key_value = pair.split('=');
            var decodedKey = decodeURIComponent(key_value[0]);
            var decodedValue = decodeURIComponent(key_value[1]);
            if (decodedKey == param) return decodedValue;
            return null;
        };

        var comparisonResult = null;

        if (search.indexOf('&') > -1) {
            var params = search.split('&');
            for (var i = 0; i < params.length; i++) {
                comparisonResult = compareKeyValuePair(params[i]);
                if (comparisonResult !== null) {
                    break;
                }
            }
        } else {
            comparisonResult = compareKeyValuePair(search);
        }

        return comparisonResult;
    }
</script>

<script type="text/javascript">
    $(document).ready(function () {
        $(".paging a").click(function () {
            window.scroll(0, 0);
        });
    });
    
</script>